package com.eightywork.life.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.eightywork.life.R;
import com.eightywork.life.activity.BrowserActivity;
import com.eightywork.life.imageCache.ImageLoader;
import com.eightywork.life.model.MyService;
import com.eightywork.life.service.BaseService;
import com.eightywork.life.service.DataService;
import com.eightywork.life.util.AndroidUtil;
import com.eightywork.life.util.FuncUtil;

public class MyServiceListAdapter extends BaseAdapter {

	private int msResource = R.layout.item_service_event;
	private LayoutInflater msInflater;
	private Context msContext;
	private ImageLoader imageLoader;
	private List<MyService> list;

	public MyServiceListAdapter(Context context, List<MyService> list) {
		msContext = context;
		msInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
		
		this.list = list;
		// TODO Auto-generated constructor stub
	}
	
	
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return (list != null)?list.size():0;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		final MyService myService = list.get(position);
		if (convertView == null) {
			holder = new ViewHolder();
			convertView  = msInflater.inflate(msResource, parent, false);
			holder.username = (TextView)convertView.findViewById(R.id.name);
			holder.name = (TextView)convertView.findViewById(R.id.append_name);
		//	holder.status = (TextView)convertView.findViewById(R.id.tv_state);
			holder.time = (TextView)convertView.findViewById(R.id.time);
		//	holder.score = (TextView)convertView.findViewById(R.id.tv_commment);
			holder.subject = (TextView)convertView.findViewById(R.id.append_title);
			holder.avatar = (ImageView)convertView.findViewById(R.id.avatar);
			holder.verifyType = (ImageView)convertView.findViewById(R.id.verify_type);
			holder.image = (ImageView)convertView.findViewById(R.id.append_image);
			holder.content = (TextView)convertView.findViewById(R.id.content);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder)convertView.getTag();
		}
		//convertView.findViewById(R.id.title).setVisibility(View.GONE);
		holder.username.setText(myService.getName());
	//	holder.status.setText(DataService.getServiceStatus(myService.getStatus()));
		holder.content.setText(myService.getContent());
		holder.name.setText("@" + myService.getName());
		holder.subject.setText(myService.getSubject());
		if (myService.getImage().startsWith("http:")) {
			imageLoader.DisplayImage(myService.getImage(), holder.image, 0, 0, 0);
		} else {
			imageLoader.DisplayImage(BaseService.DOMAIN + myService.getImage(), holder.image, 0, 0, 0);
		}
		
		int len = AndroidUtil.dpToPx(48, msContext);
		imageLoader.DisplayImage(myService.getAvatar_middle(), holder.avatar, 0, len, len);
		holder.verifyType.setBackgroundResource(DataService.getVerifyTypeRes(myService.getVerify_type()));
//		if ((Long.valueOf(myService.getYdateline()) * 1000) > System.currentTimeMillis()) {
//			holder.time.setText("预约时间: " + 
//		          FuncUtil.timeToText(Long.valueOf(myService.getDateline()) * 1000L));
//			holder.score.setVisibility(View.GONE);
//		} else {
//			holder.time.setText("完成时间: " + 
//		          FuncUtil.timeToText(Long.valueOf(myService.getDateline()) * 1000L));
//			holder.score.setVisibility(View.VISIBLE);
//			holder.score.setText(myService.getScore());
//		}
		holder.time.setText(FuncUtil.timeToText((Long.valueOf(myService.getDateline()) * 1000L)));
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent  = new Intent(msContext, BrowserActivity.class);
				intent.putExtra("url", BaseService.DOMAIN
						+ myService.getAppend_link());
				intent.putExtra("idtype", "feed");
			    msContext.startActivity(intent);
			}
		});
		return convertView;
	}


	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}
	public final class ViewHolder {
		private TextView username;
		private ImageView avatar;
		private TextView  status;
		private ImageView image;
		private ImageView verifyType;
		private TextView time;
		private TextView subject;
		private TextView score;
		private TextView name;
		private TextView content;
		
		
	}

}
